import 'package:flutter/material.dart';
import 'package:flutter_novel/config/invoke_manager.dart';
import 'package:flutter_novel/config/use_channels.dart';
import 'package:flutter_novel/detail/data/model/cartoon_recommend_entity.dart';
import 'package:flutter_novel/widget/app_net_image.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

class RecommendWidget extends StatelessWidget {
  final List<CartoonRecommendDataInfos>? list;

  const RecommendWidget({super.key, this.list});

  Widget _theMoreWidget() {
    return InkWell(
      onTap: () async {
        await InvokeManager.pushRecommendPage();
      },
      child: Container(
        color: Colors.white,
        padding: EdgeInsets.only(bottom: 16.h),
        child: Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: [
            Text(
              '更多推荐',
              style: TextStyle(
                color: const Color(0xFF000000),
                fontSize: 15.sp,
                fontWeight: FontWeight.w500,
              ),
            ),
            Image.asset(
              'assets/images/ic_arrows.png',
              width: 15.w,
              height: 20.h,
              color: const Color(0xFF586C94),
            ),
          ],
        ),
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    if ((list ?? []).isEmpty) return SizedBox();

    Widget item(int index) {
      return Column(
        children: [
          AppNetImage(
            imageUrl: list![index].cover.vCover,
            width: (1.sw - 60.w) / 4,
            height: 100.h,
            fit: BoxFit.cover,
            radius: 4.h,
          ),
          Container(
            width: (1.sw - 60.w) / 4,
            padding: EdgeInsets.only(top: 6.h),
            alignment: Alignment.centerLeft,
            child: Text(
              list![index].title,
              overflow: TextOverflow.ellipsis,
              style: TextStyle(color: Colors.black, fontSize: 12.sp),
            ),
          ),
          Container(
            width: 1.sw - 140.w,
            height: 25.h,
            alignment: Alignment.centerLeft,
            child: Text(
              list![index].author.name,
              overflow: TextOverflow.ellipsis,
              style: TextStyle(color: Colors.black54, fontSize: 11.sp),
            ),
          ),
        ],
      );
    }

    return Container(
      height: 360.h,
      width: 1.sw,
      color: Colors.white,
      padding: EdgeInsets.symmetric(horizontal: 15.w),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          _theMoreWidget(),
          Expanded(
            child: GridView.builder(
              itemCount: list!.length,
              padding: EdgeInsets.zero,
              physics: const NeverScrollableScrollPhysics(),
              gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
                childAspectRatio: 0.52, //宽高比
                crossAxisSpacing: 10.w, //水平间距
                mainAxisSpacing: 10.h, //垂直间距
                crossAxisCount: 4,
              ),
              itemBuilder: (context, index) {
                return item(index);
              },
            ),
          ),
        ],
      ),
    );
  }
}
